/*
*初始化 生产组新增下单成本显示 从订单中获取 下单成本数据同步到产品列表中
 */
package main

import (
	// "net/http"
	// "encoding/json"
	// "strconv"
	// "time"
	// "github.com/dgrijalva/jwt-go"
	// "github.com/labstack/echo"

	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	"oxy_server/model"
	"oxy_server/module/log"
)

type (
	productplan struct {
		ID               bson.ObjectId `json:"_id" bson:"_id"`
		DesignPlanID     string        `json:"designplanid" bson:"designplanid"`         //大货号
		Number           string        `json:"number" bson:"number"`                     //流水号
		LargeCargoNumber string        `json:"largecargonumber" bson:"largecargonumber"` //大货号
	}
	insertskuorder struct {
		ProductID              string `json:"productid" bson:"productid"`
		ProductctionGroupID    string `json:"productctiongroupid" bson:"productctiongroupid"`
		SkuCode                string `json:"skucode" bson:"skucode"`
		SkuDesc                string `json:"skudesc" bson:"skudesc"`
		ClothingSupplier       string `json:"clothingsupplier" bson:"clothingsupplier"`
		WatermarkNumber        string `json:"watermarknumber" bson:"watermarknumber"`
		WatermarkSupplier      string `json:"watermarksupplier" bson:"watermarksupplier"`
		WatermarkSingleMoney   string `json:"watermarksinglemoney" bson:"watermarksinglemoney"`
		CertificateNumber      string `json:"certificatenumber" bson:"certificatenumber"`
		CertificateSupplier    string `json:"certificatesupplier" bson:"certificatesupplier"`
		CertificateSingleMoney string `json:"certificatesinglemoney" bson:"certificatesinglemoney"`
		SizemarkNumber         string `json:"sizemarknumber" bson:"sizemarknumber"`
		SizemarkSupplier       string `json:"sizemarksupplier" bson:"sizemarksupplier"`
		SizemarkSingleMoney    string `json:"sizemarksinglemoney" bson:"sizemarksinglemoney"`
		Count                  string `json:"count" bson:"count"`
		OrderCount             string `json:"ordercount" bson:"ordercount"`
		Cost                   string `json:"cost" bson:"cost"`
		OrderTime              string `json:"ordertime" bson:"ordertime"`
	}
)

func getProductList() error {
	model.DB().Dbname = "OxyCms"
	model.DB().Document = "product_plan"
	query, _, err := model.DB().Query(bson.M{}, 0, 0)
	if err != nil {
		log.Error("查询出错", err)
		return err
	}
	productList := []productplan{}
	query.All(&productList)
	for _, item := range productList {
		model.DB().Dbname = "OxyCms"
		model.DB().Document = "sku_order"

		orderquery, _ := model.DB().QueryOne(bson.M{"productctiongroupid": item.ID.Hex()})
		order := insertskuorder{}
		orderquery.One(&order)

		model.DB().Document = "product_plan"
		selector := bson.M{"_id": item.ID}
		data := bson.M{"$set": bson.M{"targetprice": order.Cost}}
		err = model.DB().Update(selector, data)
		if err != nil {
			log.Error("更新出错，更新下单成本出错", err)
		}
	}
	return nil
}

func main() {

	err := getProductList()
	if err != nil {
		log.Error("查询出错", err)
		return
	}

}
